package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.PurchaseService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zhmwi
 * @Date 2021/9/4
 * @Dersciption 进货管理 期初库存
 */
@RestController
@RequestMapping("/goods")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;
    /**
     * 分页查询当前库存 带条件
     * @param page 当前页
     * @param rows 每页显示记录数
     * @param codeOrName 商品编码 或 名称
     * @param goodsTypeId 商品类型
     * @return
     */
    @ResponseBody
    @PostMapping("/listInventory")
    public Map<String,Object> getInventoryList(Integer page, Integer rows, String codeOrName, Integer goodsTypeId){
        Map result = new HashMap<>();
        PageInfo<Goods> pageInfo = purchaseService.getInventoryList(page,rows,codeOrName,goodsTypeId);
        //总件数
        long total = pageInfo.getTotal();
        //所有商品
        List<Goods> goodsList = pageInfo.getList();
        result.put("total",total);
        result.put("rows",goodsList);
        return result;
    }

    /**
     * 分页查询无库存列表  三表 库存表 商品类型表 销售集合表
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @PostMapping("/getNoInventoryQuantity")
    public Map<String,Object> getNoInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        PageInfo<Goods> pageInfo = purchaseService.getNoInventoryQuantity(page,rows,nameOrCode);
        Map result = new HashMap();
        long total = pageInfo.getTotal();
        List<Goods> goodsList = pageInfo.getList();

        result.put("total",total);
        result.put("rows",goodsList);
        return result;
    }

    /**
     * 分页查询有库存列表
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @PostMapping("/getHasInventoryQuantity")
    public Map<String,Object> getHasInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        PageInfo<Goods> pageInfo = purchaseService.getHasInventoryQuantity(page,rows,nameOrCode);
        Map result = new HashMap();
        long total = pageInfo.getTotal();
        List<Goods> goodsList = pageInfo.getList();

        result.put("total",total);
        result.put("rows",goodsList);
        return result;
    }

    /**
     * 添加库存 修改成本价
     * @param goodsId
     * @param inventoryQuantity
     * @param purchasingPrice
     * @return
     */
    @PostMapping("/saveStock")
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice){
        Boolean isSave = purchaseService.saveStock(goodsId,inventoryQuantity,purchasingPrice);
        if (isSave){
            return new ServiceVO(100,"请求成功");
        }
        return new ServiceVO(300,"请求失败");
    }

    /**
     * 删除商品（要判断商品状态 入库、有进货和销售单据的不能删除）
     * @param goodsId
     * @return
     */
    @PostMapping("/deleteStock")
    public ServiceVO deleteStock(Integer goodsId){
        Integer state = purchaseService.getGoodsStateById(goodsId);
        if (state == 1 || state == 2){
            purchaseService.updateStock(goodsId);
            return new ServiceVO(100,"请求成功");
        }
        return new ServiceVO(300,"商品状态有入库/进货和销售单据的不能删除");
    }

    /**
     * 分页查询全部商品
     * @param page
     * @param rows
     * @param goodsName
     * @param goodsTypeId
     * @return
     */
   @PostMapping("list")
   public Map<String,Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId){
       Map result = new HashMap<>();
       PageInfo<Goods> pageInfo = purchaseService.list(page,rows,goodsName,goodsTypeId);
       //总件数
       long total = pageInfo.getTotal();
       //所有商品
       List<Goods> goodsList = pageInfo.getList();
       result.put("total",total);
       result.put("rows",goodsList);
       return result;
   }

    /**
     * 查询所有 当前库存量 小于 库存下限的商品信息
     * inventory_quantity < min_num
     * @return
     */
    @PostMapping("listAlarm")
    public Map<String,Object> listAlarm(){
        Map result = new HashMap<>();
        List<Goods> goodsList = purchaseService.listAlarm();
        //所有商品
        result.put("rows",goodsList);
        return result;
    }

}
